package com.jocund.quicktest;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DynamicGroup {
	
	public DynamicGroup(){
		this.userMap = new HashMap<String, User>();
	}
	private UUID groupId;
	public void setGroupId(UUID value){
		this.groupId = value;
	}
	public UUID getGroupId(){
		return this.groupId;
	}
	
	private String name;
	public void setName(String value){
		this.name = value;
	}
	public String getName(){
		return this.name;
	}
	
	private HashMap<String, User> userMap;
//	private ArrayList<User> userList;
	public void setUserMap(HashMap<String, User> value){
		this.userMap = value;
	}
	public HashMap<String, User> getUserMap(){
		return this.userMap;
	}
	 
	public boolean getIsAlive(){
		//boolean isAlive = true;
		int countOfUserNotOnline = 0;
		for (Map.Entry<String, User>  entry: getUserMap().entrySet()){
			User user = entry.getValue();
			if (!user.getIsOnline()){
				countOfUserNotOnline++;
				if (countOfUserNotOnline >= 2)
					return false;
			}
		}
		
		return true;
	}
	
	public void addUser(User user) throws DuplicationException{
		if (this.userMap.containsKey(user.getUniqueName()))
			throw new DuplicationException("user already exists in the group!");
		this.userMap.put(user.getUniqueName(), user);
//		this.userList.add(user);
	}
	
	public void removeUser(String userUniqueName) throws NotExistException{
		if (!this.userMap.containsKey(userUniqueName))
			throw new NotExistException("user not exists in the group!");
		this.userMap.remove(userUniqueName);
//		this.userList.remove(user);
	}
	
	public void reportAlive(String userUniqueName) throws NotExistException{
		if (!this.userMap.containsKey(userUniqueName))
			throw new NotExistException("user not exists in the group!");
		User user = this.userMap.get(userUniqueName);
		user.reportOnline();
//		user1
	}
}
